﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StudentManagement.Utils;
using StudentManagement.Interfaces;
using System.Globalization;

namespace StudentManagement.UCs
{
    public partial class SearchPanelControl : UserControl
    {
        public SearchPanelControl()
        {
            InitializeComponent();

            // Add Search Type to Combo box
            cbSearchType.Items.Add(Constants.SearchName);
            cbSearchType.Items.Add(Constants.SearchBirthday);
            cbSearchType.Items.Add(Constants.SearchClass);
            cbSearchType.Items.Add(Constants.SearchAddress);

            cbSearchType.SelectedIndex = 0;
        }

        public event EventHandler<FilterEventArgs> SearchClick;
        private void InvokeSearchClick(FilterEventArgs e)
        {
            var handler = SearchClick;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string stringTypeSearch = cbSearchType.Text;
            switch (stringTypeSearch)
            {
                case Constants.SearchName:
                    InvokeSearchClick(new FilterEventArgs(txtFilter.Text, SearchType.Name));
                    break;
                case Constants.SearchBirthday:
                    DateTime datetime;
                    if (DateTime.TryParseExact(txtFilter.Text, Constants.DateTimeFormat, null, DateTimeStyles.None, out datetime))
                    {
                        InvokeSearchClick(new FilterEventArgs(txtFilter.Text, SearchType.Birthday));
                    }
                    else
                    {
                        MessageBox.Show("Input Wrong Date Time ! Pls Input dd/mm/yyyy");
                    }
                    break;
                case Constants.SearchClass:
                    InvokeSearchClick(new FilterEventArgs(txtFilter.Text, SearchType.Class));
                    break;
                case Constants.SearchAddress:
                    InvokeSearchClick(new FilterEventArgs(txtFilter.Text, SearchType.Address));
                    break;
                default:
                    break;
            }
        }
    }
}
